#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: open_website.sh
# Author: GID5564
# Description:
# Created Time: 01/12/23-21:40:48
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash


function __invoke_local_init__(){
    wait
}



function update_local_website(){
    if [[ ! -e "${WEBSITE}/.git" ]];then
        rm -rvf ${WEBSITE} 2>/dev/null
        git clone ${MYGIT}/html_doc.git ${WEBSITE}
    else
        UpdateLocalGit ${WEBSITE}
    fi
}

function open_web(){
    local port=$(rand 9999 65534)
    local cur=$(pwd)
    [[ -d $1 ]] && {
        cd $1
        invoke_web "run" "${port}"
        cd $cur
    } || error "无效目录:$1 !"
}

function open_jupyter_notebook(){
    local port=$(rand 9999 65534)

    if [[ $(command -v jupyter) ]];then
        tip "${BLUE}复制下面的网址(port: ${RESET}${YELLOW}${port}${RESET}${BLUE})到浏览器中打开或者在打开的网页上多刷新几次！按${RESET}${RED} CTRL+C ${RESET}${BLUE}退出服务。${RESET}"
        invoke_web "open_url" "${url}:${port}"
        eval "jupyter notebook --allow-root --no-browser --port ${port}"
    else
        warn "请先安装Jupyter！"
    fi
    unset port
}

function open_website(){
    local url=$1
    local port=${2:-$(rand 9999 65534)}

    local re='^(f|ht){1}(tp|tps):\/\/((\w+)\.){0,3}(\w+)(\/(\w+))*'

    [[ ${url} =~ ${re} ]] || {
        error "无效网址:${url}!"
        return 1
    }       
    
    [[ ${url} =~ '127.0.0.1' ]] && {
        invoke_web "open_url" "${url}:${port}"
    } || {
        invoke_web "open_url" "${url}"
    }

    unset url port func re
}


#♥
function get_links(){
    local keys=()
     keys=$(get_keys "${FILE_CONFIG}" "link")
    local arr=(`echo $keys | awk -v RS='' '{gsub("\n"," "); print}'`)

    for ((i=1;i<=${#arr[@]};i++))
    do
        echo $arr[$i]
    done
}




__invoke_local_init__
















